package com.haotunet.app.goldtreasure.activity;

import java.io.File;
import java.net.URISyntaxException;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.utils.LocationUtils;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ShopPositionActivity extends BaseActivity {
	private TitleView mTitleView;
	private MapView mMapView;
	private BaiduMap mBaiduMap;
	private TextView shopAddress;
	private TextView tvNavigation;
	private String address;
	private double latitude;
	private double longitude;
	private String shopName;
	private double[] bd09_To_Gcj02s;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SDKInitializer.initialize(getApplicationContext());
		setContentView(R.layout.activity_shop_position);
		initView();
	}

	private void initView() {
		Intent intent = getIntent();
		longitude = intent.getDoubleExtra("longitude", 0);
		latitude = intent.getDoubleExtra("latitude", 0);
		bd09_To_Gcj02s = LocationUtils.gcj02_To_Bd09(latitude, longitude);
		address = intent.getStringExtra("shopAddress");
		shopName = intent.getStringExtra("shopName");
		mTitleView = (TitleView) findViewById(R.id.title_view);
		mTitleView.getTitletextView().setVisibility(View.VISIBLE);
		mTitleView.getTitletextView().setText("地图");
		mTitleView.getBackView().setVisibility(View.VISIBLE);
		mTitleView.getBackView().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.showZoomControls(false);
		mMapView.removeViewAt(3);
		mBaiduMap = mMapView.getMap();
		LatLng point = new LatLng(bd09_To_Gcj02s[0], bd09_To_Gcj02s[1]);

		// 定义地图状态
		MapStatus mMapStatus = new MapStatus.Builder().target(point).zoom(18).build();
		// 定义MapStatusUpdate对象，以便描述地图状态将要发生的变化

		MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
		// 改变地图状态
		mBaiduMap.setMapStatus(mMapStatusUpdate);
		BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_marker);
		OverlayOptions option = new MarkerOptions().position(point).icon(bitmap);
		mBaiduMap.addOverlay(option);
		shopAddress = (TextView) findViewById(R.id.shop_address);
		shopAddress.setText(address);
		tvNavigation = (TextView) findViewById(R.id.txt_navigation);
		tvNavigation.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isInstallByread("com.autonavi.minimap")) {
					gotoGaode();
					return;
				} else if (isInstallByread("com.baidu.BaiduMap")) {
					gotoBaidu();
					return;
				} else {
					UIHelper.showToast(ShopPositionActivity.this, "请安装高德地图客户端!");
				}
			}
		});
	}

	private boolean isInstallByread(String packageName) {
		return new File("/data/data/" + packageName).exists();
	}

	private void gotoGaode() {
		try {
			@SuppressWarnings("deprecation")
			Intent intent = Intent.getIntent("androidamap://viewMap?sourceApplication=金有金&poiname=" + address + "&lat="
					+ latitude + "&lon=" + longitude + "&dev=0");

			startActivity(intent); // 启动调用

		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("deprecation")
	private void gotoBaidu() {
		try {

			Intent intent = Intent.getIntent("intent://map/marker?location=" + bd09_To_Gcj02s[0] + ","
					+ bd09_To_Gcj02s[1] + "&title=" + address + "&content=" + shopName + "&src=" + "金有金"
					+ "#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
			startActivity(intent);

		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}

}
